import jwt
from flask import current_app


def generate_jwt(payload, expire, secret=None):
    """
    生成jwt
    :param payload: 载荷
    :param expire:  有效期
    :param secret:  秘钥
    :return:        jwt
    """

    _payload = {'exp': expire}
    _payload.update(payload)

    if not secret:
        secret = current_app.config['JWT_SECRET']

    token = jwt.encode(_payload, secret, algorithm='HS256')
    return token.decode()


def verify_jwt(token, secret=None):
    """
    检验jwt
    :param token: jwt token
    :param secret: 秘钥
    :return: payload
    """
    if not secret:
        secret = current_app.config['JWT_SECRET']

    try:
        payload = jwt.decode(token, secret, algorithms=['HS256'])
    except jwt.PyJWTError:
        payload = None

    return payload


